package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.BalanceRefundModel;
import com.btkjgc.service.intf.BalanceRefundManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class BalanceRefundManageController {
	static Logger logger = Logger
			.getLogger(BalanceRefundManageController.class);

	@Autowired
	private BalanceRefundManageService_Intf balanceRefundManageService;

	@RequestMapping(value = "balance_refund_manage", method = {
			RequestMethod.GET, RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,HttpServletResponse response) {

		ModelAndView mv = null;
		Integer result = null;
		
		String task = request.getParameter("task");
		if ("company".equals(task)) {

			mv = new ModelAndView("balance_refund_company_manage");
			result = companyListrecord(request, response,mv);
		} else if ("person".equals(task) || StringUtils.isEmpty(task)) {

			mv = new ModelAndView("balance_refund_person_manage");
			result = personListrecord(request,response, mv);
		}

		if(result==1){
			return mv;
		}else {
			return null;
		}
	
	}

	@SuppressWarnings("unchecked")
	public Integer personListrecord(HttpServletRequest request,HttpServletResponse response,ModelAndView mv) {

		// 个人账户
		String tel = request.getParameter("tel");
		String account = request.getParameter("account");
		String begindate = request.getParameter("begindate");
		String enddate = request.getParameter("enddate");
		String status = request.getParameter("status");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "aut.account", "like", account };
		Object[] arr2 = { "aut.tel", "like", tel };
		Object[] arr3 = { "brt.createdate", "d>", begindate };
		Object[] arr4 = { "brt.createdate", "d<", enddate };
		Object[] arr5 = { "brt.status", "=", status };
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);
		paramList.add(arr5);

		String sql = "SELECT\n" +
				"	brt.*,  \n" +
				"	adut.username opername,\n" +
				"	aut.account,\n" +
				"	aut.name accountName,\n" +
				"   aut.tel,\n" +
				"	CASE brt.`status`\n" +
				"WHEN 0 THEN\n" +
				"	'不通过'\n" +
				"WHEN 1 THEN\n" +
				"	'通过'\n" +
				"WHEN 2 THEN\n" +
				"	'待审核'\n" +
				"END statusName\n" +
				"FROM\n" +
				"	balance_refund_t brt\n" +
				"LEFT JOIN account_user_t aut ON aut.id = brt.accountid and  aut.del=0\n" +
				"LEFT JOIN admin_user_t adut on adut.id=brt.operid and adut.del=0\n" +
				"AND aut.del = 0\n" +
				"WHERE\n" +
				"	brt.del = 0\n" +
				"AND brt.type = 0";

		sql = balanceRefundManageService.getwhereSQL(sql, paramList, sortfield);

		// 获取分页查询的数据结果
		totalRecord = balanceRefundManageService.getCount(sql, paramList);

		List<BalanceRefundModel> privateInfoList = balanceRefundManageService
				.getList(BalanceRefundModel.class, paramList, sql, pageIndex,
						15, sortfield);

		//是否为导出excel操作
		if("true".equals(request.getParameter("export"))){
			
			balanceRefundManageService.doDownloadAction(request, response,sql);
			
			return -1;
		}else {
			
			// 设置后台用户界面所需的其他数据
			balanceRefundManageService.setpersonListData(request, mv, pageIndex,
					totalRecord, privateInfoList);
			
			return 1;
		}

		
	
	}

	@SuppressWarnings("unchecked")
	public int companyListrecord(HttpServletRequest request,HttpServletResponse response, ModelAndView mv) {
		// 公司账户
		String account = request.getParameter("account");
		String accountid = request.getParameter("accountid");
		String begindate = request.getParameter("begindate");
		String enddate = request.getParameter("enddate");
		String status = request.getParameter("status");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "aut.account", "like", account };
		Object[] arr2 = { "aut.id", "=", accountid };
		Object[] arr3 = { "brt.createdate", "d>", begindate };
		Object[] arr4 = { "brt.createdate", "d<", enddate };
		Object[] arr5 = { "brt.status", "=", status };
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);
		paramList.add(arr5);

		String sql ="SELECT\n" +
				"	brt.*,  \n" +
				"	adut.username opername,\n" +
				"	aut.account,\n" +
				"	aut.name accountName,\n" +
				"  aut.tel,\n" +
				"	CASE brt.`status`\n" +
				"WHEN 0 THEN\n" +
				"	'不通过'\n" +
				"WHEN 1 THEN\n" +
				"	'通过'\n" +
				"WHEN 2 THEN\n" +
				"	'待审核'\n" +
				"END statusName\n" +
				"FROM\n" +
				"	balance_refund_t brt\n" +
				"LEFT JOIN account_user_t aut ON aut.id = brt.accountid and  aut.del=0\n" +
				"LEFT JOIN admin_user_t adut on adut.id=brt.operid and adut.del=0\n" +
				"AND aut.del = 0\n" +
				"WHERE\n" +
				"	brt.del = 0\n" +
				"AND brt.type = 1";

		sql = balanceRefundManageService.getwhereSQL(sql, paramList, sortfield);

		// 获取分页查询的数据结果
		totalRecord = balanceRefundManageService.getCount(sql, paramList);

		List<BalanceRefundModel> companyInfoList = balanceRefundManageService
				.getList(BalanceRefundModel.class, paramList, sql, pageIndex,
						15, sortfield);

		// 设置后台用户界面所需的其他数据
		balanceRefundManageService.setCompanyList(request, mv, pageIndex,
				totalRecord, companyInfoList);
		
		if("true".equals(request.getParameter("export"))){
			
			balanceRefundManageService.doDownloadAction(request, response,sql);
			return -1;
			
		}else {
			return 1;
		}
	
	}

	
	//换电记录退款小票打印信息
	@RequestMapping(value = "get_balance_refund_info_by_id")
	@ResponseBody
	public BalanceRefundModel getAccountinfoById(HttpServletRequest request ) {
		String type = request.getParameter("type");
		String accountid = request.getParameter("id");
		String sql ="SELECT\n" +
				" brt.*,  \n" +
				" adut.username opername,\n" +
				" aut.account,\n" +
				" aut.name accountName,\n" +
				" aut.tel,\n" +
				" ait.available_balance balance\n"+
				" FROM\n" +
				" balance_refund_t brt\n" +
				" LEFT JOIN account_user_t aut ON aut.id = brt.accountid and  aut.del=0\n" +
				" LEFT JOIN admin_user_t adut on adut.id=brt.operid and adut.del=0\n" +
				" left JOIN account_info_t ait ON ait.accountid=aut.id\n" +
				" AND aut.del = 0\n" +
				" WHERE\n" +
				" brt.del = 0\n" +
				" AND brt.type = "+type+"\n"+
				" AND brt.id = ?";
		
		BalanceRefundModel balanceRefundModel = (BalanceRefundModel) HibernateUtil
				.executeSQLQueryObject(sql, BalanceRefundModel.class, accountid);
		
		return balanceRefundModel;
	}
	
	@RequestMapping(value = "del_balance_refund", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String delBalanceRefund(HttpServletRequest request,
			String id) {

		String task = request.getParameter("task");
		balanceRefundManageService.updateDelById(id);

		return "redirect:/balance_refund_manage?task="+task;
	}
	

	@RequestMapping(value = "check_balance_refund", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String checkBalanceRefund(HttpServletRequest request,
			String id,String status,String checkreason) {
		
		String task = request.getParameter("task");
		balanceRefundManageService.updateStatusById(id, status, checkreason);

		return "redirect:/balance_refund_manage?task="+task;
	}
	
	@RequestMapping(value = "get_account_by_id")
	@ResponseBody
	public BalanceRefundModel getAccountById(HttpServletRequest request,
			String id) {
		
	
		BalanceRefundModel balanceRefundModel=balanceRefundManageService.getAccountById(id);

	    return balanceRefundModel;
	}

	@RequestMapping(value = "add_balance_refund/{a}")
	@ResponseBody
	public   AjaxObj jsonAddBalanceRefund(HttpServletRequest request,
			@PathVariable("a") int a ,BalanceRefundModel balanceRefundModel) {
 
		  
		  AjaxObj ajaxObj = balanceRefundManageService
					.insertBalanceRefundModelModel(balanceRefundModel);
 
		return ajaxObj;
	}

}